<?php
/**
 * gpa_demo Description
 * @package
 * @name Database
 * @filesource database.php
 * @author Meta Sanjaya
 */

class MySQL {
    /**
     * @access private
     * @var resource $_mysql_id
     */
    private $_mysql_id;

    /**
     * @access private
     * @var String $_query
     */
    private $_query;
    
    /**
     * @access private
     * @var String $_table_prefix
     */
    private $_table_prefix;

    /**
     * @access private
     * @var int $_insert_id
     */
    private $_insert_id;

    /**
     * @access private
     * @var int $_affected_rows
     */
    private $_affected_rows;

    /**
     * @access private
     * @var Object $_last_result;
     */
    private $_last_result;
    
    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct() {}

    /**
     * @access public
     * @method connect
     * @param String $host
     * @param String $user
     * @param String $pass
     * @param String $db
     * @param String $prefix
     * @return void
     */
    public function connect($host, $user, $pass, $database, $prefix="") {
        if(is_resource($this->_mysql_id = mysql_pconnect($host, $user, $pass))) {
            if(false!=mysql_select_db($database, $this->_mysql_id)) {
                $this->_query = "";
                $this->_last_result = null;
                $this->_insert_id = 0;
                $this->_affected_rows = 0;
                $this->_table_prefix = $prefix;
            } else {
                trigger_error(mysql_error());
                exit;
            }
        } else {
            trigger_error(mysql_error());
            exit;
        }
    }

    /**
     * @access public
     * @method query
     * @param String
     * @return Object
     */
    public function query() {
        $args = func_get_args();
        $str = array_shift($args);
        if(!count($args)) {
            $args = array();
        }

        $this->_query = $this->parse_str_query($str, $args);

        //var_dump($this->getQuery());
        //exit;

        try {
            if(!preg_match('/^(insert|update|delete|create|drop)/i', $this->getQuery())) {
                $_result = mysql_query($this->getQuery(), $this->_mysql_id);
                $this->_last_result = new stdClass;
                $this->_last_result->fields = @mysql_fetch_field($_result);
                $this->_last_result->rows = array();

                while($row = @mysql_fetch_assoc($_result)) {
                    $this->_last_result->rows[] = $row;
                }

                @mysql_free_result($_result);
            } else {
                mysql_unbuffered_query($this->getQuery(), $this->_mysql_id);
                $this->_insert_id = intval(@mysql_insert_id());
                $this->_affected_rows = intval(@mysql_affected_rows());
                $this->_last_result = true;
            }

            return $this->getResult();
        } catch(Exception $e) {
            trigger_error($e->getMessage());
            exit;
        }
    }

    /**
     * @access public
     * @method getQuery
     * @param void
     * @return String
     */
    public function getQuery() {
        return $this->_query;
    }

    /**
     * @access public
     * @method getResult
     * @param void
     * @return Object
     */
    public function getResult() {
        return $this->_last_result;
    }

    /**
     * @access public
     * @method getInsertId
     * @param void
     * @return int
     */
    public function getInsertId() {
        return $this->_insert_id;
    }

    /**
     * @access public
     * @method getAffectedRows
     * @param void
     * @return int
     */
    public function getAffectedRows() {
        return $this->_affected_rows;
    }

    /**
     * @access public
     * @method escape
     * @param String $str
     * @return String
     */
    public function escape($str="") {
        return mysql_real_escape_string($str, $this->_mysql_id);
    }

    /**
     * @access private
     * @method parse_str_query
     * @param String $str
     * @param array $array
     * @return String
     */
    private function parse_str_query($str, $array=array()) {
        $return = "";
        $eval = "\$return = sprintf(\"" . preg_replace("|#_|", $this->_table_prefix, $str) . "\"";
        foreach($array as $arg) {
            $eval .= ", " . $arg;
        }
        $eval .= ");";
        //die($eval);
        eval($eval);
        return $return;
    }

    /**
     * @access public
     * @method __destruct
     * @param void
     * @return void
     */
    public function  __destruct() {
        @mysql_close($this->_mysql_id);
    }
}

?>